﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace HYRElectric.Equipment
{
	public partial class SwitchBoxesLabelForm : System.Windows.Forms.Form
	{
		public SwitchBoxesLabelForm()
		{
			this.InitializeComponent();
		}

		private void SwitchBoxesLabelForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.cmbSortStyle.Items.Add("升序");
			this.cmbSortStyle.Items.Add("降序");
			if (!this.ReadFormDataFromXml())
			{
				this.InitDlgDataByDefault();
			}
		}

		private bool ReadFormDataFromXml()
		{
			string text = Product.WorkingLocation + "SwitchBoxesLabelForm.xml";
			if (!File.Exists(text))
			{
				return false;
			}
			try
			{
				YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(text, "Root");
				xmlHelper.SelectNode("Root\\Datas");
				this.txtHeadNum.Text = xmlHelper.GetValueEx("LabelPrefix", "AH");
				this.txtStartNum.Text = xmlHelper.GetValueEx("StartNum", "1");
				if (xmlHelper.GetValue("SortStyle") == "升序")
				{
					this.cmbSortStyle.SelectedIndex = 0;
				}
				else
				{
					this.cmbSortStyle.SelectedIndex = 1;
				}
				int x = Convert.ToInt32(xmlHelper.GetAttributeEx("FormPosX", "100"));
				int y = Convert.ToInt32(xmlHelper.GetAttributeEx("FormPosY", "100"));
				base.Location = new System.Drawing.Point(x, y);
			}
			catch
			{
				return false;
			}
			return true;
		}

		private void WriteFormData2Xml()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = workingLocation + "SwitchBoxesLabelForm.xml";
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("LabelPrefix", this.txtHeadNum.Text);
			xmlHelper.SetValue("StartNum", this.txtStartNum.Value.ToString());
			xmlHelper.SetValue("SortStyle", this.cmbSortStyle.SelectedItem.ToString());
			xmlHelper.SetAttribute("FormPosX", base.Location.X.ToString());
			xmlHelper.SetAttribute("FormPosY", base.Location.Y.ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void InitDlgDataByDefault()
		{
			this.txtHeadNum.Text = "AH";
			this.txtStartNum.Value = 1m;
			this.cmbSortStyle.SelectedIndex = 0;
			base.Location = new System.Drawing.Point(100, 100);
		}

		public string Prefix
		{
			get
			{
				return this.txtHeadNum.Text;
			}
		}

		public int StartNum
		{
			get
			{
				return Convert.ToInt32(this.txtStartNum.Text.Trim());
			}
		}

		public bool SortAsc
		{
			get
			{
				return this.cmbSortStyle.SelectedIndex == 0;
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SwitchBoxesLabelForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SwitchBoxesLabelForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			SwitchBoxesLabelForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
			this.WriteFormData2Xml();
		}
	}
}
